package tv.dyndns.kishibe.server.sns;

import java.sql.SQLException;

import tv.dyndns.kishibe.client.packet.PacketProblem;
import tv.dyndns.kishibe.server.database.Database;

import com.google.inject.Inject;
import com.restfb.DefaultFacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;

public class FacebookClient implements SnsClient {
	private com.restfb.FacebookClient facebookClient;

	@Inject
	public FacebookClient(Database database) {
		String accessToken = null;

		try {
			accessToken = database.getPassword("facebook_access_token");
		} catch (SQLException e) {
			e.printStackTrace();
		}

		this.facebookClient = new DefaultFacebookClient(accessToken);
	}

	@Override
	public void postProblem(PacketProblem problem) {
		String problemReportSentence = problem.getProblemReportSentence();
		String message = "問題番号" + problem.id + ":" + problemReportSentence
				+ " http://kishibe.dyndns.tv/qmaclone/";
		post(message);
	}

	@Override
	public void postThemeModeUpdate(String theme) {
		String message = "テーマモード 「" + theme + "」 が更新されました" + " http://kishibe.dyndns.tv/qmaclone/";
		post(message);
	}

	private void post(String message) {
		facebookClient.publish("me/feed", FacebookType.class, Parameter.with("message", message));
	}

	@Override
	public void followBack() {
		throw new UnsupportedOperationException();
	}
}
